package gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.swing.*;
//此类构建管理员对景点间的边进行增删改查的界面
public class ManagerEdge extends JFrame {
	private JPanel managerEdgePanel;

	public ManagerEdge() {
		super("道路管理窗口");
		setSize(1000, 800);
		setLocation(200, 150);

		Font font = new Font("宋体", Font.PLAIN, 25);
		UIManager.put("OptionPane.font", font);
		UIManager.put("OptionPane.messageFont", font);
		UIManager.put("OptionPane.buttonFont", font);

		setVisible(true);
	}

	function.Edge edge = new function.Edge();
	function.Scenic scenic = new function.Scenic();

	public void managerEdgePanel() {
		managerEdgePanel = new JPanel();
		managerEdgePanel.setLayout(null);

		ButtonGroup group0 = new ButtonGroup();
		/*将每个边做成一个JRadioButton
		 * 组成一个ButtonGroup显示在界面左侧
		 * 管理员可通过选择不同边而容易地对边进行增删改查
		 */
		ArrayList<String> list = edge.display();
		for (int i = 0; i < list.size(); i++) {
			JRadioButton rb = new JRadioButton(list.get(i), true);
			rb.setBounds(50, 50 + 30 * i, 300, 30);
			rb.setFont(new Font("宋体", Font.PLAIN, 25));
			managerEdgePanel.add(rb);
			group0.add(rb);
		}

		JLabel informationLabel = new JLabel("");
		informationLabel.setBounds(400, 350, 400, 150);
		informationLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		managerEdgePanel.add(informationLabel);

		JLabel codeLabel = new JLabel("code");
		codeLabel.setBounds(400, 100, 60, 30);
		codeLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		managerEdgePanel.add(codeLabel);

		JLabel codeText = new JLabel("");
		codeText.setBounds(480, 100, 100, 30);
		codeText.setFont(new Font("宋体", Font.PLAIN, 25));
		managerEdgePanel.add(codeText);
		//将景点id和name组成的字符串作为元素放入list中,用于构建JComboBox
		ArrayList<String> list1 = scenic.display();
		//s数组中存放的是景点的名称
		String[] s = new String[list1.size()];
		for (int i = 0; i < list1.size(); i++) {
			s[i] = list1.get(i).split("  ")[1];
		}
		
		JLabel scenic1Label = new JLabel("scenic1");
		scenic1Label.setBounds(400, 150, 100, 30);
		scenic1Label.setFont(new Font("宋体", Font.PLAIN, 25));
		managerEdgePanel.add(scenic1Label);

		JComboBox<String> scenic1Text = new JComboBox(s);
		scenic1Text.setBounds(500, 150, 200, 30);
		scenic1Text.setFont(new Font("宋体", Font.PLAIN, 20));
		managerEdgePanel.add(scenic1Text);

		JLabel scenic2Label = new JLabel("scenic2");
		scenic2Label.setBounds(400, 200, 100, 30);
		scenic2Label.setFont(new Font("宋体", Font.PLAIN, 25));
		managerEdgePanel.add(scenic2Label);

		JComboBox<String> scenic2Text = new JComboBox(s);
		scenic2Text.setBounds(500, 200, 200, 30);
		scenic2Text.setFont(new Font("宋体", Font.PLAIN, 20));
		managerEdgePanel.add(scenic2Text);

		JLabel distanceLabel = new JLabel("distance");
		distanceLabel.setBounds(400, 250, 120, 30);
		distanceLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		managerEdgePanel.add(distanceLabel);

		JTextField distanceText = new JTextField(5);
		distanceText.setBounds(540, 250, 100, 30);
		distanceText.setFont(new Font("宋体", Font.PLAIN, 25));
		managerEdgePanel.add(distanceText);

		JButton buttonAdd = new JButton("添加");
		buttonAdd.setBounds(80, 700, 150, 40);
		buttonAdd.setFont(new Font("宋体", Font.PLAIN, 25));
		managerEdgePanel.add(buttonAdd);

		buttonAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//自动为新添加的边分配编号，编号为已有边数+1，边的编号是连续的
				codeText.setText(Integer.toString(edge.getTotalNumber() + 1));
				//使用含有四个参数的构造方法实例化要添加的edge对象
				edge = new function.Edge(codeText.getText(), scenic1Text.getSelectedItem().toString(),
						scenic2Text.getSelectedItem().toString(), distanceText.getText());
				boolean a = edge.add();
				//若添加成功（此时a==true）则同时将此边加入按钮组在窗口中显示出来
				if (a) {
					/*新建一个JRadioButton，将新添加的边
					 * 也加入初始的buttongroup中在页面左侧显示出来
					 */
					ArrayList<String> list2 = edge.display();
					JRadioButton rb = new JRadioButton(codeText.getText() + "  " + scenic1Text.getSelectedItem().toString() + "  "+ scenic2Text.getSelectedItem().toString(),true);
					rb.setBounds(50, 50 + 30 * (list2.size() - 1), 300, 30);
					rb.setFont(new Font("宋体", Font.PLAIN, 25));
					managerEdgePanel.add(rb);
					group0.add(rb);
					//添加成功后清除界面
					codeText.setText("");
					distanceText.setText("");
//					提示添加是否成功
					JOptionPane.showMessageDialog(null, "添加成功", "提示", JOptionPane.ERROR_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(null, "添加失败", "提示", JOptionPane.ERROR_MESSAGE);
				}
			}
		});

		JButton button2 = new JButton("删除");
		button2.setBounds(250, 700, 100, 40);
		button2.setFont(new Font("宋体", Font.PLAIN, 25));
		managerEdgePanel.add(button2);
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String enable = "false";
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns = group0.getElements();
				AbstractButton btn;
				while (radioBtns.hasMoreElements()) {
					btn = radioBtns.nextElement();
					if (btn.isSelected()) {
						enable = btn.getText();
						group0.remove(btn);
						btn.setVisible(false);
						break;
					}
				}
				//调用edge类的delete方法删除景点
				boolean a = edge.delete(enable);
				if (a) {
					JOptionPane.showMessageDialog(null, "删除成功", "提示", JOptionPane.ERROR_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(null, "删除失败", "提示", JOptionPane.ERROR_MESSAGE);
				}

			}
		});
		/*在修改时，管理员选中要修改的边，
		 * 先点击“修改”按钮，会将被选中的边的内容显示出来
		 然后管理员可在已有内容上进行修改，
		 修改完毕后点击“保存修改”按钮即可将修改保存到edge.xml中
		 	*/
		JButton button3 = new JButton("修改");
		button3.setBounds(380, 700, 100, 40);
		button3.setFont(new Font("宋体", Font.PLAIN, 25));
		managerEdgePanel.add(button3);
		button3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String enable = "false";
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns = group0.getElements();
				while (radioBtns.hasMoreElements()) {
					AbstractButton btn = radioBtns.nextElement();
					if (btn.isSelected()) {
						enable = btn.getText();
						break;
					}
				}
				//调用edge类的find方法返回按钮选中的edge对象
				edge = edge.find(enable);
				//将此边的信息打印出来
				codeText.setText(edge.getCode());
				scenic1Text.setSelectedItem(edge.getScenic1());
				scenic2Text.setSelectedItem(edge.getScenic2());
				distanceText.setText(edge.getDistance());
			}
		});
		
		JButton buttonSaveModify = new JButton("保存修改");
		buttonSaveModify.setBounds(500, 700, 180, 40);
		buttonSaveModify.setFont(new Font("宋体", Font.PLAIN, 25));
		managerEdgePanel.add(buttonSaveModify);
		buttonSaveModify.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String enable = "false";
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns = group0.getElements();
				AbstractButton btn;
				while (radioBtns.hasMoreElements()) {
					btn = radioBtns.nextElement();
					if (btn.isSelected()) {
						enable = btn.getText();
						break;
					}
				}

				edge = new function.Edge(codeText.getText(), scenic1Text.getSelectedItem().toString(),
						scenic2Text.getSelectedItem().toString(), distanceText.getText());
//				调用edge类的modify方法实现修改
				boolean a = edge.modify(enable);
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns1 = group0.getElements();
				if (a) {
					while (radioBtns1.hasMoreElements()) {
						btn = radioBtns1.nextElement();
						if (btn.isSelected()) {
							btn.setText(codeText.getText() + "  " + scenic1Text.getSelectedItem() + "  "
									+ scenic2Text.getSelectedItem());
							break;
						}
					}
					//修改成功后清除界面
					codeText.setText("");
					scenic1Text.setSelectedItem("");
					scenic2Text.setSelectedItem("");
					distanceText.setText("");

					JOptionPane.showMessageDialog(null, "修改成功", "提示", JOptionPane.ERROR_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(null, "修改失败", "提示", JOptionPane.ERROR_MESSAGE);
				}
			}
		});

		JButton button = new JButton("查询");
		button.setBounds(700, 700, 110, 40);
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		managerEdgePanel.add(button);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String enable = "false";
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns = group0.getElements();
				while (radioBtns.hasMoreElements()) {
					AbstractButton btn = radioBtns.nextElement();
					if (btn.isSelected()) {
						enable = btn.getText();
						break;
					}
				}
				//调用edge类的inquire方法实现查询
				String information = edge.inquire(enable);
				informationLabel.setText(information);
			}
		});
		//点击离开按钮关闭当前窗口
		JButton button4 = new JButton("离开");
		button4.setBounds(840, 700, 100, 40);
		button4.setFont(new Font("宋体", Font.PLAIN, 25));
		managerEdgePanel.add(button4);
		button4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});

		managerEdgePanel.setVisible(true);
		add(managerEdgePanel);
		setVisible(true);
	}
}
